package org.example.study.liaoxuefeng;

import org.testng.annotations.Test;

/**
 * @author: 默苍璃
 * @date: 2023-05-23 11:54
 * <p>
 * 守护线程
 */
public class DaemonThread {

    public static void main(String[] args) {
        Thread t = new Thread1();
        //设置t为守护线程 如果注释下一行，观察Thread1的执行情况:
        t.setDaemon(true);
        t.start();
        System.out.println("main: wait 3 sec...");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        System.out.println("main: end.");
    }


    /**
     * 守护线程
     */
    @Test
    public void test() throws InterruptedException {
        Thread t = new Thread1();
        // 如果注释下一行，观察Thread1的执行情况:
        t.setDaemon(true);
        t.start();
        System.out.println("main: wait 3 sec...");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        System.out.println("main: end.");
    }


}


class Thread1 extends Thread {

    public void run() {
        for (; ; ) {
            System.out.println("Thread-1: running...");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    }

}
